package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.Home;
import com.ipfsemc.ipfsemc.service.IHomeService;
import com.ipfsemc.ipfsemc.swagger.ApiModelPropertyIgnore;
import com.ipfsemc.ipfsemc.util.JsonData;
import com.ipfsemc.ipfsemc.util.RespList;
import com.ipfsemc.ipfsemc.util.Response;
import com.ipfsemc.ipfsemc.util.ResponseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@Api(value = "HomeController", tags = "首页")

@RequestMapping("home")
public class HomeController {
    private static final Logger logger = LoggerFactory.getLogger(ApplyController.class);

    @Autowired
    private IHomeService homeService;


    @PostMapping("/addHome")
    @ApiOperation(value = "添加首页连接",httpMethod = "POST")
    public ResponseBase uploadWork(MultipartFile file, String homeAddress)  {
        ResponseBase jsonData=new ResponseBase();
        if (!file.isEmpty()) {
            try {
                if (file == null) {
                    jsonData.setMessage("请选择要上传的图片");
                }
                if (file.getSize() > 1024 * 1024 * 10) {
                   jsonData.setMessage("文件大小不能大于10M");
                }
                //获取文件后缀
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
                if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) {
                    jsonData.setMessage("请选择jpg,jpeg,gif,png格式的图片");
                }
                String savePath = "D:\\image";
                File savePathFile = new File(savePath);
                if (!savePathFile.exists()) {
                    //若不存在该目录，则创建目录
                    savePathFile.mkdir();
                    System.out.println("我是不存在得！！！！！");
                }
                //通过UUID生成唯一文件名
                String filename = UUID.randomUUID().toString().replaceAll("-","") + "." + suffix;

                try {
                    //将文件保存指定目录
                    file.transferTo(new File(savePath + "\\"+filename));
                } catch (Exception e) {
                    e.printStackTrace();
                    jsonData.setMessage("保存文件异常");
                }

                //添加至数据库
                Home home=new Home();
                home.setHomeAddress(homeAddress);
                home.setHomeImg(filename);
                int add = homeService.add(home);
                if (add>0){
                    jsonData.setCode(0);
                    jsonData.setMessage("添加成功！");
                }else {
                    jsonData.setMessage("添加失败！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("[home][addHome]", e);
                jsonData.setMessage("tu");
            }
        }
        return jsonData;

    }

    @ApiOperation(value = "删除首页连接",httpMethod = "POST")
    @RequestMapping(value = "delHome",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="homeId",value="首页ID",required=true,dataType="Integer"),
    })
    public ResponseBase delHome(Integer homeId){
        ResponseBase jsonData=new ResponseBase();
        try {
            if (null ==homeId ){
                jsonData.setMessage("参数有误");
            }else{
                int del = homeService.del(homeId);
                if(del>0){
                    jsonData.setCode(0);
                    jsonData.setMessage("删除成功！");
                }
                jsonData.setMessage("删除失败！");
            }

        } catch (Exception ex) {
            logger.error("[home][delHome]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }


        return jsonData;
    }

    @ApiOperation(value = "修改首页连接",httpMethod = "POST")
    @RequestMapping(value = "updateHome",method = RequestMethod.POST)
    public ResponseBase updateHome(Home home){
        ResponseBase jsonData=new ResponseBase();
        try {
            if (null ==home.getHomeId()) {
                jsonData.setMessage("参数有误");
            }else{
                int update = homeService.update(home);
                if(update>0){
                    jsonData.setCode(0);
                    jsonData.setMessage("修改成功！");
                }
                jsonData.setMessage("修改失败！");

            }

        } catch (Exception ex) {
            logger.error("[home][updateHome]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }



        return jsonData;
    }

    @ApiOperation(value = "查询全部首页连接",httpMethod = "GET")
    @RequestMapping(value = "listHome",method = RequestMethod.GET)

    public RespList<Home> listHome(){
        RespList jsonData=new RespList();
        try {
            List<Home> list = homeService.list(null);
            jsonData.setResult(list);
            jsonData.setCode(0);

        } catch (Exception ex) {
            logger.error("[home][listHome]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }
}
